/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CarWatchUtilities.java
 *
 * Created on 29/07/2010, 12:13:35 PM
 */

package utilities;
import java.awt.*;
import java.awt.image.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;
import javax.imageio.ImageIO;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.dom4j.*;
import org.dom4j.io.*;

import javax.swing.text.AttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;


/**
 *
 * @author ATuan
 */
public class CarWatchUtilities extends javax.swing.JDialog {

    private static Logger theLogger =
	 Logger.getLogger(CarWatchUtilities.class.getName());
    private static FileHandler fileTxt;
    private static FileHandler fileXML;

    private static SimpleFormatter formatterTxt;
    private String sURL="";
    private Image image = null;
    
    HTMLEditorKit.ParserCallback callback;

    /** Creates new form CarWatchUtilities */
    public CarWatchUtilities(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try{
        formatterTxt = new SimpleFormatter();
        //formatterTxt = new XMLFormatter();
        fileTxt = new FileHandler("CarWatchUtilities.log",true);
        fileTxt.setFormatter(formatterTxt);
        theLogger.addHandler(fileTxt);
        }catch(Exception e){
            theLogger.severe(e.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        URLTF = new javax.swing.JTextField();
        GoBtn = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        CarDetailsTA = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        panel1 = new java.awt.Panel();
        jButton1 = new javax.swing.JButton();
        CarsList = new java.awt.List();
        jButton2 = new javax.swing.JButton();
        CarDetailsList = new java.awt.List();

        jFrame1.setName("jFrame1"); // NOI18N

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(placecommframework.PlaceCommFrameworkApp.class).getContext().getResourceMap(CarWatchUtilities.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        URLTF.setText(resourceMap.getString("URLTF.text")); // NOI18N
        URLTF.setName("URLTF"); // NOI18N

        GoBtn.setText(resourceMap.getString("GoBtn.text")); // NOI18N
        GoBtn.setName("GoBtn"); // NOI18N
        GoBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                GoBtnMouseClicked(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        CarDetailsTA.setColumns(20);
        CarDetailsTA.setRows(5);
        CarDetailsTA.setName("CarDetailsTA"); // NOI18N
        jScrollPane2.setViewportView(CarDetailsTA);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        panel1.setName("panel1"); // NOI18N

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 361, Short.MAX_VALUE)
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 263, Short.MAX_VALUE)
        );

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        CarsList.setName("CarsList"); // NOI18N
        CarsList.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                CarsListItemStateChanged(evt);
            }
        });
        CarsList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CarsListActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        CarDetailsList.setName("CarDetailsList"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CarsList, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                            .addComponent(URLTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CarDetailsList, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE))))
                .addGap(85, 85, 85)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(GoBtn)
                    .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(113, 113, 113))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(URLTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(GoBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(CarsList, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(CarDetailsList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46))
                    .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void GoBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GoBtnMouseClicked
        // TODO add your handling code here:
        String sUrl = URLTF.getText().trim();

        ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();
     callback = new ReportAttributes(this,false );

        try {
          URL u = new URL(sUrl);
          InputStream in = u.openStream();
          InputStreamReader r = new InputStreamReader(in);
          parser.parse(r, callback, false);
        } catch (IOException e) {
          System.out.print("Error:")  ;
          System.err.println(e);
          System.out.println();
        }

    }//GEN-LAST:event_GoBtnMouseClicked

    public void paint(Graphics g) {
    g.drawImage( image, 0, 0, null);
  }
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:

        String sURL="http://www.governmentauctions.com.au/photo.aspx?m=2917706&n=1&s=F";
        try {
           // Read from a file
        //File sourceimage = new File("source.gif");
        //image = ImageIO.read(sourceimage);

        // Read from an input stream
        //InputStream is = new BufferedInputStream(
        //    new FileInputStream("source.gif"));
        //image = ImageIO.read(is);

        // Read from a URL
        URL url = new URL(sURL);
        image = ImageIO.read(url);
        panel1.setVisible(true);
        } catch (IOException e) {
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void CarsListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CarsListActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CarsListActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        CarsList.add("Test");
    }//GEN-LAST:event_jButton2MouseClicked

    private void CarsListItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_CarsListItemStateChanged
        // TODO add your handling code here:

        System.out.println("Item ["+CarsList.getSelectedIndex()+"] is selected");
        String sCarDetails=CarsList.getSelectedItem().trim();
        String sVehicleID=sCarDetails.substring(sCarDetails.indexOf("vehicle.aspx"),sCarDetails.length());
        System.out.println("sVehicleID:"+sVehicleID);

        String sUrl = URLTF.getText().trim();
        
        String sBaseURL="http://www.governmentauctions.com.au";
        System.out.println("sBaseURL:"+sBaseURL);
        String sVehicleUrl=sBaseURL+"/"+sVehicleID;
        CarDetailsTA.append(sVehicleUrl+"\n");
        
        
        ParserGetter kit = new ParserGetter();
        HTMLEditorKit.Parser parser = kit.getParser();
        callback = new ReportAttributes(this, true);
        try {
          URL u = new URL(sVehicleUrl);
          InputStream in = u.openStream();
          InputStreamReader r = new InputStreamReader(in);
          parser.parse(r, callback, false);
        } catch (IOException e) {
          System.out.print("Error:")  ;
          System.err.println(e);
          System.out.println();
        }
        


    }//GEN-LAST:event_CarsListItemStateChanged

    public void addCarToCarlist (String sVehicleID){
        CarsList.add(sVehicleID);


    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CarWatchUtilities dialog = new CarWatchUtilities(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.List CarDetailsList;
    private javax.swing.JTextArea CarDetailsTA;
    private java.awt.List CarsList;
    private javax.swing.JButton GoBtn;
    private javax.swing.JTextField URLTF;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private java.awt.Panel panel1;
    // End of variables declaration//GEN-END:variables

    void SetCarDetails(String sCarDetails) {
        CarDetailsList.add(sCarDetails);
    }

}
